/*
 * GUIManager.cpp
 *
 *  Created on: Sep 12, 2013
 *      Author: jason
 */

#include <iostream>
#include "../../include/GUI/GUIManager.hpp"

namespace OForce_UI {

MenuWindow::MenuWindow() : mSelectedIcon(-1), mHighLightedIcon(0)
{
	sf::Vector2f viewCenter = view.getCenter();
	sf::Vector2f viewSize   = view.getSize();
	viewSize.x /= 2;
	viewSize.y /= 2;
	if (!mUp.loadFromFile("data/Images/Icons/search.png")) {
		std::cout << "Could not load search icon.\n";
	}
	if (!mDown.loadFromFile("data/Images/Icons/status.png")) {
		std::cout << "Could not load status icon.\n";
	}
	if (!mLeft.loadFromFile("data/Images/Icons/magic.png")) {
		std::cout << "Could not load magic icon.\n";
	}
	if (!mRight.loadFromFile("data/Images/Icons/item.png")) {
		std::cout << "Could not load item icon.\n";
	}
	mUpSprite.setTexture(mUp);
	mUpSprite.setTextureRect(sf::IntRect (sf::Vector2i(0,0), sf::Vector2i(64,64)));
	mUpSprite.setPosition(viewCenter.x-32,viewCenter.y+viewSize.y-148);
	mDownSprite.setTexture(mDown);
	mDownSprite.setTextureRect(sf::IntRect (sf::Vector2i(0,0), sf::Vector2i(64,64)));
	mDownSprite.setPosition(viewCenter.x-32,viewCenter.y+viewSize.y-56);
	mLeftSprite.setTexture(mLeft);
	mLeftSprite.setTextureRect(sf::IntRect (sf::Vector2i(0,0), sf::Vector2i(64,64)));
	mLeftSprite.setPosition(viewCenter.x-96,viewCenter.y+viewSize.y-116);
	mRightSprite.setTexture(mRight);
	mRightSprite.setTextureRect(sf::IntRect (sf::Vector2i(0,0), sf::Vector2i(64,64)));
	mRightSprite.setPosition(viewCenter.x,viewCenter.y+viewSize.y-116);
}

MenuWindow::~MenuWindow()
{

}

void MenuWindow::draw()
{
	Window.draw(mUpSprite);
	Window.draw(mDownSprite);
	Window.draw(mLeftSprite);
	Window.draw(mRightSprite);
}

void MenuWindow::update()
{
	// TODO update the currently highlighted sprite
	// or if a selection is made, change all sprites
}

void MenuWindow::selectIcon(int i)
{
	// TODO
}

DialogueWindow::DialogueWindow() :
		typingSpeed(200.0),
		curTime(0),
		endTime(0),
		mFinished(false) {
	if (!mImage.loadFromFile("data/primitiveTextBox.png"))
		std::cout << "Could not load text window image\n";
	mImage.createMaskFromColor(sf::Color(19, 95, 181), 200);
	mTexture.loadFromImage(mImage);
	mSprite.setTexture(mTexture);
//	mSprite.setPosition(0.0, viewCenter.y+viewSize.y-220.0f);
	mSprite.setScale(0.9, 0.5);
	windowText.setFont(font);
	windowText.setCharacterSize(24);
	windowText.setStyle(1);
}

DialogueWindow::~DialogueWindow()
{

}

void DialogueWindow::setString(std::string text)
{
	// TODO automatically format text for window
	mText = text;
	curTime = 0;
	endTime = 1000.0*text.size() / typingSpeed;
	sf::Vector2f viewCenter = view.getCenter();
	sf::Vector2f viewSize   = view.getSize();
	viewSize.x /= 2;
	viewSize.y /= 2;
	mSprite.setPosition(viewCenter.x-viewSize.x+20.0,
				viewCenter.y+viewSize.y-220.0f);
	windowText.setPosition(viewCenter.x-viewSize.x+50.0f,
					viewCenter.y+viewSize.y-200.0f);
}

void DialogueWindow::drawText()
{
	int drawnChars = 0;
	// How much of the text has been drawn out of 1.0 (normalized)
	float norm_finished;
	int curChar; // how many characters to draw this time
	std::string textToDraw;
	if (mFinished) {
		norm_finished = 1.0f;
	} else {
		norm_finished = curTime / endTime;
	}
	curChar = norm_finished * mText.size();
	textToDraw = mText.substr(0, curChar);
	windowText.setString(textToDraw);
	Window.draw(windowText);
}

void DialogueWindow::draw()
{
	Window.draw(mSprite);
	drawText();
}

void DialogueWindow::update()
{
	curTime += dt*typingSpeed;
}

void DialogueWindow::hurry()
{
	curTime += 1.0 / (2*typingSpeed);
}

}
